#include "glFont.h"

void glFont::BuildFont(void) // Build Our Font Display List
{
	float cx; // Holds Our X Character Coord
	float cy; // Holds Our Y Character Coord

	base = glGenLists(256); // Creating 256 Display Lists
	glBindTexture(GL_TEXTURE_2D, tex); // Select Our Font Texture
	for (loop = 0; loop < 256; loop++) // Loop Through All 256 Lists
	{
		cx = float(loop % 16) / 16.0f; // X Position Of Current Character
		cy = float(loop / 16) / 16.0f; // Y Position Of Current Character

		glNewList(base + loop, GL_COMPILE); // Start Building A List
		glBegin(GL_QUADS); // Use A Quad For Each Character
		glTexCoord2f(cx, 1 - cy - 0.0625f); // Texture Coord (Bottom Left)
		glVertex2i(0, 0); // Vertex Coord (Bottom Left)
		glTexCoord2f(cx + 0.0625f, 1 - cy - 0.0625f); // Texture Coord (Bottom Right)
		glVertex2i(16, 0); // Vertex Coord (Bottom Right)
		glTexCoord2f(cx + 0.0625f, 1 - cy); // Texture Coord (Top Right)
		glVertex2i(16, 16); // Vertex Coord (Top Right)
		glTexCoord2f(cx, 1 - cy); // Texture Coord (Top Left)
		glVertex2i(0, 16); // Vertex Coord (Top Left)
		glEnd(); // Done Building Our Quad (Character)
		glTranslated(13, 0, 0); // Move To The Right Of The Character
		glEndList(); // Done Building The Display List

	} // Loop Until All 256 Are Built
}

void glFont::KillFont(void) // Delete The Font From Memory
{
	glDeleteLists(base, 256); // Delete All 256 Display Lists
}

void glFont::glPrint(GLint x, GLint y, int set, const char *fmt, ...) // Where The Printing Happens
{
	if (set > 1)
	{
		set = 1;
	}

	y = (Engine::screenSettings.screenHeight - y) - 16;

	char text[256];
	va_list ap;

	va_start(ap, fmt);
	// Parses The String For Variables
	vsprintf(text, fmt, ap); // And Converts Symbols To Actual Numbers
	va_end(ap);

	char *string = text;

	glEnable(GL_TEXTURE_2D);
	glEnable(GL_BLEND);
	glBindTexture(GL_TEXTURE_2D, tex); // Select Our Font Texture
	glTexEnvf(GL_BLEND, GL_RGB, 0.0);
	glDisable(GL_LIGHTING);
	glDisable(GL_DEPTH_TEST); // Disables Depth Testing
	glMatrixMode(GL_PROJECTION); // Select The Projection Matrix
	glPushMatrix(); // Store The Projection Matrix
	glLoadIdentity(); // Reset The Projection Matrix
	glOrtho(0, Engine::screenSettings.screenWidth, 0,
			Engine::screenSettings.screenHeight, -1, 1); // Set Up An Ortho Screen
	glMatrixMode(GL_MODELVIEW); // Select The Modelview Matrix
	glPushMatrix(); // Store The Modelview Matrix
	glLoadIdentity(); // Reset The Modelview Matrix
	glTranslated(x, y, 0); // Position The Text (0,0 - Bottom Left)
	glListBase(base - 32 + (128 * set)); // Choose The Font Set (0 or 1)
	glCallLists(strlen(string), GL_BYTE, string); // Write The Text To The Screen
	glMatrixMode(GL_PROJECTION);
	glPopMatrix(); // Restore The Old Projection Matrix
	glMatrixMode(GL_MODELVIEW); // Select The Modelview Matrix
	glPopMatrix(); // Restore The Old Projection Matrix
	glEnable(GL_DEPTH_TEST); // Enables Depth Testing
	glEnable(GL_LIGHTING);
	glDisable(GL_BLEND);
	glDisable(GL_TEXTURE_2D);
}

glFont::glFont(std::string _filename)
{
	//BuildFont();
	filename = _filename;

	base = 0;
	loop = 0;

	if (Textures::Font_textures != NULL)
	{
		Textures::Font_textures->loadDir();
		tex = Textures::Font_textures->getTexIdByName(filename);
	}
	else
	{
		tex = 0;
		std::cerr << "Unable to load Font Texture Pool" << std::endl;
	}
}

glFont::~glFont()
{
	KillFont();
}

